# Contributor: Łukasz Jendrysik <scadu@yandex.com>
# Contributor: Michał Polański <michal@polanski.me>
# Maintainer: Leonardo Arena <rnalrd@alpinelinux.org>
pkgname=ulogd
pkgver=2.0.8
pkgrel=3
pkgdesc="Userspace logging daemon for netfilter/iptables related logging"
url="https://netfilter.org/projects/ulogd/"
arch="all"
license="GPL-2.0-only"
makedepends="
	bsd-compat-headers
	jansson-dev
	libmnl-dev
	libnetfilter_acct-dev
	libnetfilter_conntrack-dev
	libnetfilter_log-dev
	libnfnetlink-dev
	libpcap-dev
	libpq-dev
	linux-headers
	mariadb-connector-c-dev
	openssl-dev>3
	sqlite-dev
	zlib-dev
	"
subpackages="
	$pkgname-doc
	$pkgname-openrc
	"
source="https://www.netfilter.org/projects/ulogd/files/ulogd-$pkgver.tar.bz2
	ulogd.initd
	ulogd.confd
	ulogd.logrotate
	"
options="!check"

_modules="mysql pgsql json xml sqlite3"
for _module in $_modules; do
	subpackages="$subpackages $pkgname-$_module:_plugin"
done

prepare() {
 	default_prepare
	update_config_sub
}

build() {
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr \
		--sysconfdir=/etc \
		--mandir=/usr/share/man \
		--infodir=/usr/share/info \
		--with-pgsql \
		--with-mysql
	make
}

package() {
	make DESTDIR="$pkgdir" install
	install -Dm644 ulogd.conf "$pkgdir"/etc/ulogd.conf
	install -Dm755 "$srcdir"/$pkgname.initd "$pkgdir"/etc/init.d/$pkgname
	install -Dm644 "$srcdir"/$pkgname.confd "$pkgdir"/etc/conf.d/$pkgname
	install -Dm644 "$srcdir"/$pkgname.logrotate "$pkgdir"/etc/logrotate.d/$pkgname
	install -dm755 "$pkgdir"/usr/share/doc/ulogd
	install -Dm644 ulogd.conf "$pkgdir"/usr/share/doc/ulogd/ulogd.conf.sample
	install -Dm644 doc/ulogd.sgml "$pkgdir"/usr/share/doc/ulogd/ulogd.sgml
	install -dm0755 "$pkgdir"/usr/share/ulogd
	for file in $(ls doc/*.sql); do
		install -Dm644 $file "$pkgdir"/usr/share/ulogd/$file
	done
}

_plugin() {
	plugin="${subpkgname##*-}"
	depends="$pkgname=$pkgver-r$pkgrel"
	pkgdesc="$plugin output for $pkgname"

	mkdir -p "$subpkgdir"/usr/lib/ulogd
	mv "$pkgdir"/usr/lib/ulogd/ulogd_output_"$(echo $plugin | tr '[a-z]' '[A-Z]')".so \
		"$subpkgdir"/usr/lib/ulogd

	# Move the .sql files to their plugins to subpackage
	case "$plugin" in
		pgsql|mysql)
			mkdir -p "$subpkgdir"/usr/share/ulogd/doc
			mv "$pkgdir"/usr/share/ulogd/doc/"$plugin"* "$subpkgdir"/usr/share/ulogd/doc
			;;
	esac
}
sha512sums="
9f99f6f35bad5da4559d788dc3ba3dae17d4ae972737cae3313ecf68f08eaf5f55514fce6f30503437e4158fd30a06438b9249d5d20f6343964cbf690f87309d  ulogd-2.0.8.tar.bz2
1aef18f3c2f67231e9fd4ac3c8121ed337905e810714f817bc605b08f230396159d98072011d2aae4556dd241997d6abdba9fb804c3ae1b681a489c429662227  ulogd.initd
0908e82de8b1a7be9281c4749d9cf396b6ef70b3eee868d83e390ffcea3bb2c4854351e0fde4be831282ec54da694bc4e4b144c2b2e13b314c625a8c10084e6d  ulogd.confd
82768ec7ffc53f82f4914d80c38da0a269fcc4e68b7955746c1d6c1e4f3fce48a927b4f29b5929d7fc2a01701160c41e05960bbeb1fd682705d589912789e69e  ulogd.logrotate
"
